package com.hyou.springboot.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 权限校验拦截器相关Bean配置类
 * 
 * @author HyouSeki
 * @version 1.0.0 2018-12-17 14:36:25 初始创建
 */
@Configuration
public class AuthInterceptorConfig implements WebMvcConfigurer {
    
    private static final Logger log = LoggerFactory.getLogger(AuthInterceptorConfig.class);
    
    @Bean
    @ConfigurationProperties(prefix = "hyou.auth.intceptor")
    public AuthInterceptorProp authInterceptorProp() {
        return new AuthInterceptorProp();
    }
    
    @Bean
    public AuthInterceptor authInterceptor() {
        return new AuthInterceptor();
    }
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        AuthInterceptorProp prop = authInterceptorProp();
        log.info("authInterceptorProp : {}", prop);
        
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(authInterceptor());
        
        if (!CollectionUtils.isEmpty(prop.getExcludes())) {
            interceptorRegistration.excludePathPatterns(prop.getExcludes());
        }
        
        if (!CollectionUtils.isEmpty(prop.getIncludes())) {
            interceptorRegistration.addPathPatterns(prop.getIncludes());
        } else {
            interceptorRegistration.addPathPatterns("/**");
        }
    }
    
}
